﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models.world.vos.actors.machines {
    import flash.events.*;
    import flash.geom.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;

    public class TreatmentMachineVO extends AbstractMachineVO implements ISpeaking {

        private var _time:int;

        private static var _stateOrder:Object = initStateOrder();

        public function TreatmentMachineVO(_arg1:Object, _arg2:IEventDispatcher){
            super(_arg1, _arg2);
            speechBubblePosition = new Point(-20, -190);
            this.checkState();
        }
        override public function checkState():void{
            switch (state){
                case WorldConstants.CHARGED:
                    this.onCharged(true);
                    break;
                case WorldConstants.DIRTY:
                    this.handleDirtyState();
                    break;
            };
        }
        override public function get isFree():Boolean{
            var _local1:Boolean = (((((state == WorldConstants.CLEAN)) || ((state == WorldConstants.CHARGING)))) || ((state == WorldConstants.CHARGED)));
            return (((_local1) && ((_patient == null))));
        }
        override public function handlePatient(_arg1:ITreatable):void{
            if (_arg1.state != WorldConstants.PATIENT){
                dispatch(new InfoEvent(EventConstants.ENFORCE_NEXT_PATIENT_ACTION, _arg1.id, ((("Patient " + _arg1.id) + " is not a patient but ") + _arg1.state)));
            } else {
                if (_arg1.currentTreatment.roomType != type){
                    dispatch(new InfoEvent(EventConstants.ENFORCE_NEXT_PATIENT_ACTION, _arg1.id, (("Patient " + _arg1.id) + " is in the wrong treatment room")));
                } else {
                    if (_patient){
                        _arg1.moveToNextTarget(GameConstants.TYPE_WAITING_ROOM);
                        dispatch(new InfoEvent(EventConstants.REPORT_PROBLEM, null, ((("There is already Patient " + _patient.id) + " in this treatment room and no place for ") + _arg1.id)));
                    } else {
                        _patient = _arg1;
                        switch (state){
                            case WorldConstants.CLEAN:
                                this.handleCleanState();
                                break;
                            case WorldConstants.CHARGING:
                                this.handleChargingState();
                                break;
                            case WorldConstants.CHARGED:
                                this.handleChargedState();
                                break;
                            case WorldConstants.WORKING:
                                this.handleWorkingState();
                                break;
                            case WorldConstants.FINISHED:
                                this.handleFinishedState();
                                break;
                            case WorldConstants.DIRTY:
                                this.handleDirtyState();
                                break;
                        };
                        dispatch(updateEvent);
                    };
                };
            };
        }
        private function handleCleanState():void{
            this.putPatientIntoMachine(true);
            this.showBubble(SpeechBubbleConstants.ICON_BATTERY);
        }
        private function handleChargingState():void{
            this.putPatientIntoMachine(true);
        }
        private function handleChargedState():void{
            this.putPatientIntoMachine(true);
            if (_operator.isTreating){
                this.state = WorldConstants.WORKING;
            } else {
                this.showSunFeedHint();
            };
        }
        private function handleWorkingState():void{
            this.putPatientIntoMachine(true);
            if (patient.currentTreatment.finishedIn){
                dispatch(new InfoEvent(EventConstants.ON_TREATMENT_STARTED, _patient.id, _patient));
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
                this.dispatchWorkingEvents();
            } else {
                this.state = WorldConstants.FINISHED;
            };
        }
        private function handleFinishedState():void{
            this.putPatientIntoMachine(true);
            this.showBubble(SpeechBubbleConstants.ICON_READY);
            _operator.animationState = GameConstants.ANIMATION_STATE_STILL;
        }
        private function handleDirtyState():void{
            this.showBubble(SpeechBubbleConstants.ICON_DIRTY);
        }
        override public function getAction(_arg1:Object):InfoEvent{
            var _local2:InfoEvent;
            switch (state){
                case WorldConstants.CLEAN:
                case WorldConstants.FINISHED:
                case WorldConstants.DIRTY:
                    _local2 = new InfoEvent(EventConstants.SET_NEXT_TREATMENT_STATE, null, this);
                    break;
            };
            return (_local2);
        }
        public function get treatmentTimeLeft():int{
            if (((_patient) && (_patient.currentTreatment))){
                return (_patient.currentTreatment.finishedIn);
            };
            return (-1);
        }
        override public function set state(_arg1:String):void{
            super.state = _arg1;
            switch (state){
                case WorldConstants.CLEAN:
                    this.setCleanState();
                    break;
                case WorldConstants.CHARGING:
                    this.setChargingState();
                    return;
                case WorldConstants.WORKING:
                    this.setWorkingState();
                    break;
                case WorldConstants.FINISHED:
                    this.setFinishedState();
                    break;
                case WorldConstants.DIRTY:
                    this.setDirtyState();
                    break;
                default:
                    return;
            };
            dispatch(updateEvent);
        }
        private function setCleanState():void{
            this.updateMachine(this.onClean);
            if (_patient){
                this.showBubble(SpeechBubbleConstants.ICON_BATTERY);
            } else {
                removeSpeechBubble();
            };
        }
        private function setChargingState():void{
            dispatch(new InfoEvent(EventConstants.CHARGE_MACHINE, roomId, this.onCharged));
        }
        private function onCharged(_arg1:Boolean):void{
            super.state = (_arg1) ? WorldConstants.CHARGED : WorldConstants.CLEAN;
            if (_arg1){
                removeSpeechBubble();
            };
            if (((((_arg1) && (_patient))) && (_operator.isTreating))){
                this.state = WorldConstants.WORKING;
            } else {
                if (_arg1){
                    this.checkForDoctorFeedHint();
                };
                dispatch(updateEvent);
            };
        }
        private function setWorkingState():void{
            var _local1:Object;
            if (((_patient) && (_patient.currentTreatment))){
                this.dispatchWorkingEvents();
                removeSpeechBubble();
                _local1 = {};
                _local1[ProtocolConstants.ROOM_ID] = roomId;
                this.updateTreatment(_patient, WorldConstants.START, this.onTreatmentStarted, _local1);
            };
        }
        private function dispatchWorkingEvents():void{
            _operator.animationState = GameConstants.ANIMATION_STATE_USE;
        }
        private function setFinishedState():void{
            _operator.animationState = GameConstants.ANIMATION_STATE_STILL;
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            this.showBubble(SpeechBubbleConstants.ICON_READY);
            dispatch(new InfoEvent(EventConstants.ON_TREATMENT_FINISHED, _patient.id, _patient));
        }
        private function setDirtyState():void{
            this.updateTreatment(_patient, WorldConstants.FINALIZE, this.onFinalize);
            this.showBubble(SpeechBubbleConstants.ICON_DIRTY);
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
        }
        override protected function handleTick(_arg1:int):void{
            this._time = _arg1;
            var _local2:ITreatment = _patient.currentTreatment;
            if (_local2.finishedIn <= 0){
                this.state = WorldConstants.FINISHED;
            } else {
                _local2.finishedIn--;
            };
        }
        private function updateMachine(_arg1:Function=null):void{
            var _local2:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, _arg1);
            _local2.addRequest("clean_machine", {id:roomId});
            dispatch(_local2);
        }
        private function updateTreatment(_arg1:ITreatable, _arg2:String, _arg3:Function, _arg4:Object=null):void{
            var _local5:UpdateTreatmentEvent = new UpdateTreatmentEvent(EventConstants.UPDATE_TREATMENT, _arg1, _arg2, _arg3, _arg4);
            dispatch(_local5);
        }
        private function onTreatmentStarted(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:ITreatment;
            var _local4:String;
            if (_arg1.success){
                _local2 = _arg1.data.start_treatment;
                _local3 = _patient.currentTreatment;
                _local3.startedAt = _local2[ProtocolConstants.STARTED_AT];
                _local3.finishedAt = _local2[ProtocolConstants.FINISHED_AT];
                _local3.finishedIn = _local2[ProtocolConstants.FINISHED_IN];
                dispatch(new InfoEvent(EventConstants.ON_TREATMENT_STARTED, _patient.id, _patient));
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            } else {
                _local4 = ((("Treatment for " + _patient.id) + " could not be started: ") + _arg1.status);
                dispatch(new InfoEvent(EventConstants.ENFORCE_NEXT_PATIENT_ACTION, _patient.id, _local4));
            };
        }
        private function onClean(_arg1:ResultVO):void{
            if (_arg1.success){
                dispatch(new InfoEvent(EventConstants.HANDLE_NEXT_IN_QUEUE, type));
                dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
            };
        }
        private function onFinalize(_arg1:ResultVO):void{
            var _local2:String;
            var _local3:ITreatable;
            var _local4:InfoEvent;
            if (_arg1.success){
                this.putPatientIntoMachine(false);
                _local2 = _patient.id;
                _local3 = _patient;
                _patient = null;
                dispatch(new InfoEvent(EventConstants.ON_TREATMENT_FINALIZED, _local2, _arg1.data[WorldConstants.FINALIZE]));
                dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_GET_MONEY));
                if (((_local3.isNewDisease) && (!(_local3.currentTreatment)))){
                    _local4 = new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.FIRST_CURED_POPUP, _local3);
                    dispatch(_local4);
                };
                dispatch(updateEvent);
            };
        }
        private function putPatientIntoMachine(_arg1:Boolean):void{
            _patient.isVisible = !(_arg1);
            var _local2:String = (_arg1) ? EventConstants.HIDE_VIEW : EventConstants.SHOW_VIEW;
            var _local3:InfoEvent = new InfoEvent(_local2, _patient.id);
            dispatch(_local3);
        }
        private function showBubble(_arg1:String):void{
            showSpeechBubble(SpeechBubbleConstants.TYPE_ICON_BUBBLE, {icon:_arg1});
        }
        public function checkForDoctorFeedHint():void{
            if (((_operator) && (!(_operator.isTreating)))){
                this.showSunFeedHint();
            };
        }
        private function showSunFeedHint():void{
            dispatch(new InfoEvent(EventConstants.SET_SUN_SPEECH_BUBBLE, "sun_message_getcoffee"));
        }

        private static function initStateOrder():Object{
            var _local1:Object = {};
            _local1[WorldConstants.FINISHED] = WorldConstants.DIRTY;
            _local1[WorldConstants.DIRTY] = WorldConstants.CLEAN;
            _local1[WorldConstants.CLEAN] = WorldConstants.CHARGING;
            return (_local1);
        }
        public static function getNextState(_arg1:String):String{
            return (_stateOrder[_arg1]);
        }

    }
}//package net.wooga.pets.models.world.vos.actors.machines 
